package org.lookahead.parent.apidoc.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.lookahead.parent.apidoc.constant.HeaderConstant;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


@Slf4j
@Configuration
public class LaFeignRequestHeaderInterceptor implements RequestInterceptor {


    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            requestTemplate.header(HeaderConstant.TOKEN, request.getHeader(HeaderConstant.TOKEN));
            log.debug("feign add header:{}", request.getHeader(HeaderConstant.TOKEN));
        }
    }
}
